package dao;
import java.io.Serializable;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

public class HibernateDao<T> implements InterfaceDao<T> {

	private Class<T> classe;

	public HibernateDao(Class<T> classe) {
		super();
		this.classe = classe;
	}

	public EntityManager createEntityManager() {
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("e");
		EntityManager em = emf.createEntityManager();
		return em;
	}

	@Override
	public void salvar(T bean) {
		EntityManager em = createEntityManager();
		em.getTransaction().begin();
		em.persist(bean);
		em.getTransaction().commit();
		em.close();
	}

	@Override
	public void atualizar(T bean) {
		EntityManager em = createEntityManager();
		em.getTransaction().begin();
		em.persist(bean);
		bean = em.merge(bean);
		em.getTransaction().commit();
		em.close();
	}

	@Override
	public void excluir(T bean) {
		EntityManager em = createEntityManager();

		em.getTransaction().begin();
	//	bean = em.find(getClasse(), id);
		em.remove(bean);
		em.getTransaction().commit();
		em.close();

	}

	@Override
	public T getBean(Serializable T) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<T> getBean() {
		// TODO Auto-generated method stub
		return null;
	}

	public Class<T> getClasse() {
		return classe;
	}

	public void setClasse(Class<T> classe) {
		this.classe = classe;
	}

}
